<?php

/**
 * @project thecontactspace - ContactSpace is cloud based real time mobile contact synchronization service.
 * @module contactspace services
 * @filename MailEngine.php
 * @functionality to email
 * @author Green Mile <kuvic16@gmail.com>
 * @created-date 5-Mar-2013
 * @last-modified-date 5-Mar-2013
 * @project-status under construction
 */
require_once "Mail.php";

class MailEngine extends CApplicationComponent{

    public static function sendForgotPassowrdError($to, $conformation_code, $displayname) {
        try {
            $from = Yii::app()->params->email['from'];
            $subject = "[thecontactspace] Please reset your password";
            $body = "Dear ".$displayname.",\n\n";
            $body = $body."Please, follow the link: ".Yii::app()->request->getBaseUrl(true)."/site/forgot_recover/$conformation_code to reset your password.\n\n";
            $body .= "This link will be valid for next one hour.\n";
            $body = $body."Best Regards,\n";
            $body = $body."The Contact Space Team\n";
            $body = $body."V for Vagabond Lab\n";
            
            $host = Yii::app()->params->email['host'];
            $port = Yii::app()->params->email['port'];
            $username = Yii::app()->params->email['username'];
            $password = Yii::app()->params->email['password'];

            $headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
            $smtp = Mail::factory('smtp', array('host' => $host,
                        'port' => $port,
                        'auth' => true,
                        'username' => $username,
                        'password' => $password));

            $mail = $smtp->send($to, $headers, $body);

            if (PEAR::isError($mail)) {
                // log that email failed
                return false;
            } else {
                return true;
            }
        } catch (Exception $e) {
            return false;
        }
    }


    public static function sendConfirmationRequestEmail($to, $conformation_code, $displayname) {
        try {
            $from = Yii::app()->params->email['from'];
            $subject = "[viewinvesta] Please varify your account";
            $body = "Dear ".$displayname.",\n\n";
            $body = $body."Thanks for choosing us. Your account have been created. We are waiting for your confirmation.\n\n";
            $body = $body."Confirmation code: ".$conformation_code."\n\n";
            $body = $body."Please, follow the link: ".Yii::app()->request->getBaseUrl(true).Yii::app()->params->email['confrimation_url']."\n\n";
            $body = $body."Best Regards,\n";
            $body = $body."The Viewinvesta Team\n";
            
            $host = Yii::app()->params->email['host'];
            $port = Yii::app()->params->email['port'];
            $username = Yii::app()->params->email['username'];
            $password = Yii::app()->params->email['password'];

            $headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
            $smtp = Mail::factory('smtp', array('host' => $host,
                        'port' => $port,
                        'auth' => true,
                        'username' => $username,
                        'password' => $password));

            $mail = $smtp->send($to, $headers, $body);

            if (PEAR::isError($mail)) {
                // log that email failed
                return false;
            } else {
                return true;
            }
        } catch (Exception $e) {
            return false;
        }
    }

}

?>
